package com.example.crm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm.entity.SalesTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 销售任务Mapper接口
 */
@Mapper
public interface SalesTaskMapper extends BaseMapper<SalesTask> {
    
    /**
     * 分页查询任务列表（包含关联信息）
     * @param page 分页参数
     * @param params 查询参数
     * @return 分页结果
     */
    IPage<SalesTask> selectTasksByPage(Page<SalesTask> page, @Param("params") Map<String, Object> params);
    
    /**
     * 查询指定用户的任务数量统计（按状态分组）
     * @param userId 用户ID
     * @return 状态统计结果
     */
    @Select("SELECT status, COUNT(*) as count FROM crm_sales_task " +
            "WHERE (owner_id = #{userId} OR creator_id = #{userId}) AND deleted = 0 " +
            "GROUP BY status")
    List<Map<String, Object>> countTasksByStatus(@Param("userId") Long userId);
    
    /**
     * 查询今日到期的任务
     * @param userId 用户ID
     * @return 任务列表
     */
    @Select("SELECT t.*, u1.real_name as owner_name, u2.real_name as creator_name FROM crm_sales_task t " +
            "LEFT JOIN sys_user u1 ON t.owner_id = u1.id " +
            "LEFT JOIN sys_user u2 ON t.creator_id = u2.id " +
            "WHERE t.owner_id = #{userId} AND t.status IN (0, 1) AND t.deleted = 0 " +
            "AND DATE(t.due_date) = CURDATE() " +
            "ORDER BY t.priority ASC, t.due_date ASC")
    List<SalesTask> selectTodayTasks(@Param("userId") Long userId);
} 